#include "AudioSource.h"
#include "AudioManager.h"

using namespace game_engine;
using namespace game_engine::components;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

AudioSource::AudioSource(void)
    : Component(), sourceName(), audioFile(), audioType(EFFECT), loop(false), 
    volume(0.1f), mute(false)
{
    type = "AudioSource";
}

AudioSource::AudioSource(GameObject *obj, const string &name, const AudioType &t)
    : Component(obj), sourceName(name), audioFile(), audioType(t), loop(false), 
    volume(0.1f), mute(false)
{
    type = "AudioSource";
}

AudioSource::~AudioSource(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void AudioSource::initialize(void) throw(...)
{
    AudioManager *audioManager = AudioManager::getInstance();
    audioManager->createAudioSource(sourceName, audioFile, true);
    audioManager->setSourceVolume(sourceName, volume);
}

void AudioSource::play(void) throw(...)
{
    AudioManager *audioManager = AudioManager::getInstance();
    if (!mute)
    {
        audioManager->playSource2D(sourceName, loop);
    }
}

void AudioSource::stop(void) throw(...)
{
    AudioManager *audioManager = AudioManager::getInstance();
    audioManager->stopSource(sourceName);
}

void AudioSource::pause(void) throw(...)
{
    AudioManager *audioManager = AudioManager::getInstance();
    audioManager->pauseSource(sourceName);
}

///////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////

const AudioType& AudioSource::getAudioType(void) const
{
    return audioType;
}

const FLOAT32 AudioSource::getVolume(void) const
{
    return volume;
}

const BOOL AudioSource::getMute(void) const
{
    return mute;
}

///////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////

void AudioSource::setAudioFile(const string &file)
{
    audioFile = file;
}

void AudioSource::setLoop(const bool l)
{
    loop = l;
}

void AudioSource::setVolume(const FLOAT32 vol)
{
    volume = vol;
    AudioManager *audioManager = AudioManager::getInstance();
    audioManager->setSourceVolume(sourceName, volume);
}

void AudioSource::setMute(const bool m)
{
    mute = m;
}
